<?php

/**
 * cat
 *  name
 *  parent
 * data
 *  id
 *  name
 *  url
 *  cat
 *  blank
 */
define("DATA_PATH","tools.json");
$action = isset($_REQUEST["act"]) ? $_REQUEST["act"] : '';
# 参数请求
$db = new DB();
switch ($action){
    case 'add':
        if(IS_AJAX()){
            add($_POST,$db);
        }else{
            add();
        }
        break;
    case 'save':
        break;
    case 'del':
        break;
    default:
        select($db);
}
# 添加表单
function add($arg = null,$db = null){
    if($arg){
        $data = $db->select();
        $count = count($data["data"]);
        $postData = [
            'id' => sprintf("%s%05s",time(),++$count),
            'name' => $arg['name'],
            'link' => $arg['link'],
            'blank' => $arg['blank'],
            'cat' => 0,
        ];
        $res = $db->insert($postData);
        if($res){
            echo json_encode(["status"=>0,"msg"=>"新增数据成功！"]);
        }else{
            echo json_encode(["status"=>1,"msg"=>"新增数据失败！"]);
        }
        die;
    }else{
        $doc = <<<EOF
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        
        <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css">
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    </head>
    <body>
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-row">
                <form class="layui-form" action="">
                  <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                      <input type="text" name="name" required  lay-verify="required" placeholder="请输入显示文字" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">链接</label>
                    <div class="layui-input-block">
                      <input type="text" name="link" required  lay-verify="required" placeholder="请输入链接" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">新窗口</label>
                    <div class="layui-input-block">
                      <input type="checkbox" name="blank" lay-skin="switch" checked>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <div class="layui-input-block">
                      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                  </div>
                </form>
            </div>
            
        </div>
    </div>
    <!-- 你的HTML代码 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.min.js"></script>
    <script>
        //一般直接写在一个js文件中
        layui.use(['layer', 'form'], function(){
            var layer = layui.layer
                ,form = layui.form;
          //监听提交
          form.on('submit(formDemo)', function(data){
              $.post({
                url:'?act=add',
                data:data.field,
                dataType: "json",
                success:function(res) {
                    result = res.toString()
                    if(res.status){
                        layer.msg(res.msg)
                        return false;
                    }
                    layer.msg(res.msg)
                    setTimeout(function() {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭  
                    },1000)
                }
              });
            return false;
          });
        });
    </script>
    </body>
    </html>
EOF;
        echo $doc;
    }
}

# 数据展示
function select($db){

    $res = $db->select();
//    echo "<pre>";
//    print_r($res);
//    echo "</pre>";
# 默认显示页面
    # 数据展示
    $doc = <<<EOF
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>本地工具合集</title>
    
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="layui-container">
    <div class="layui-row">
        <ul class="layui-nav" lay-filter="">
          <li class="layui-nav-item">
            <a href="javascript:;" class="add-tools"><i class="layui-icon">&#xe608;</i> 新增导航</a>
          </li>
        </ul>
    </div>
    <div class="layui-row">
        <blockquote class="layui-elem-quote">工具导航</blockquote>
        <div class="layui-row">
            
        </div>
        <blockquote class="layui-elem-quote">本地项目导航</blockquote>
        <div class="layui-row">
            
        </div>
        
    </div>
</div>
<!-- 你的HTML代码 -->
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.min.js"></script>
<script>
    //一般直接写在一个js文件中
    layui.use(['layer', 'form'], function(){
        var layer = layui.layer
            ,form = layui.form;

        // layer.msg('Hello World');
        $(".add-tools").click(function() {
            layer.open({
              type: 2,
              title: '新增导航',
              shadeClose: true,
              shade: false,
              maxmin: true, //开启最大化最小化按钮
              area: ['400px', '300px'],
              content: '?act=add',
              end:function(){
                  layer.msg("数据已更新，立即重载")
              }
            });
        })
    });
</script>
</body>
</html>
EOF;
    echo $doc;
}

class DB{
    public function __construct()
    {
        if(!file_exists(DATA_PATH)){
            $this->save(["cat"=>[],"data"=>[]]);
        }
    }
    # 数据插入
    public function insert($params)
    {
        $data = $this->select();
        array_push($data["data"],$params);

        $res = $this->save($data);
        return $res;

    }

    # 数据修改
    public function update($data)
    {

    }

    # 数据删除
    public function delete()
    {

    }

    # 数据获取
    public function select()
    {
        $data = file_get_contents(DATA_PATH);
        return json_decode($data,1);
    }

    private function save($data)
    {
        $res = file_put_contents(DATA_PATH,json_encode($data));
        if($res > 0){
            return true;
        }
        else{
            return false;
        }
    }
}
# 判断是否ajax
function IS_AJAX(){
    if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
        // ajax 请求的处理方式
        return true;
    }else{
        // 正常请求的处理方式
        return false;
    };
}
?>
<!---->
<!--<dd><a href="phpinfo.php">phpinfo</a></dd>-->
<!--<dd><a href="dbinfo.php">dbinfo</a></dd>-->
<!--<dd><a href="WxGroupsTools/statistics.html">社群数据统计</a></dd>-->
